package de.openlaboratory.elib.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.openlaboratory.elib.core.ApplicationResources;
import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.dispatcher.DefaultRequestDispatcher;

public class DefaultFrontController extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8765455176120696796L;

	// Initializes the servlet.
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
	}

	/** Returns a short description of the servlet */
	public String getServletInfo()
	{
		return "Default Front Controller for the openlaboratory.de eLib system.";
	}

	// Destroys the servlet.
	public void destroy()
	{
	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			java.io.IOException
	{
		String page;

		/**
		 * ApplicationResources provides a simple API for retrieving constants
		 * and other preconfigured values
		 **/
		ApplicationResources resource = ApplicationResources.getInstance();

		try
		{
			// Use a helper object to gather parameter specific information.
			RequestHelper helper = new RequestHelper(request);

			Command cmdHelper = helper.getCommand();

			// Command helper perform custom operation
			page = cmdHelper.execute(request, response);

			LogManager.logMessage("Command answer page: " + page);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			LogManager.logMessage("FrontController threw Exception : " + e.toString());
			// request.setAttribute(resource.getMessageAttr(),
			// "Exception occurred : " + e.getMessage());
			page = resource.getErrorPage(e);
		}

		// dispatch control to view
		dispatch(request, response, page);
	}

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			java.io.IOException
	{
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			java.io.IOException
	{
		processRequest(request, response);
	}

	protected void dispatch(HttpServletRequest request, HttpServletResponse response, String page)
			throws ServletException, IOException
	{
		DefaultRequestDispatcher dispatcher = new DefaultRequestDispatcher(page);
		dispatcher.forward(request, response);
	}
}